package com.paomi.client.activity.detail.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;


import com.paomi.client.R;
import com.paomi.client.activity.detail.model.PlanBean;

import java.util.ArrayList;
import java.util.List;


/**
 * Created by pc on 2018/3/13.
 * 时间选择的列表适配
 */

public class MyListAdapter extends RecyclerView.Adapter<MyListAdapter.ViewHolder> {
    Context context;
    ArrayList<PlanBean> timeData = new ArrayList<>();
    List<PlanBean> mlist;
    MyListAdapter.OnItemClick onClickListener;

    public void OnItemClick(MyListAdapter.OnItemClick onClickListener) {
        this.onClickListener = onClickListener;
    }


    public MyListAdapter(Context context, List<PlanBean> list) {
        this.context = context;
        this.mlist = list;
    }

    @Override
    public MyListAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new MyListAdapter.ViewHolder(LayoutInflater.from(context).inflate(R.layout.item_calendar_time, parent, false));
    }

    @Override
    public void onBindViewHolder(final MyListAdapter.ViewHolder holder, final int position) {
        PlanBean ticket=mlist.get(position);
        holder.calendar_time.setText(ticket.getName());
        if(ticket.getType()==0){
            holder.calendar_time.setBackgroundResource(R.drawable.bg_ticket_un);
            holder.calendar_time.setTextColor(context.getResources().getColor(R.color.text_66));
        }else if(ticket.getType()==1){
            holder.calendar_time.setBackgroundResource(R.drawable.bg_ticket_on);
            holder.calendar_time.setTextColor(context.getResources().getColor(R.color.main_color));
        }
        holder.time_lay.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(onClickListener!=null){
                    for (int i=0;i<mlist.size();i++){
                        if (i==position){
                            mlist.get(i).setType(1);
                        }else{
                            mlist.get(i).setType(0);
                        }
                    }
                    notifyDataSetChanged();
                    onClickListener.setOnItemClick(holder.time_lay,position);
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return mlist.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        TextView calendar_time;
        LinearLayout time_lay;

        public ViewHolder(View itemView) {
            super(itemView);
            time_lay = (LinearLayout) itemView.findViewById(R.id.time_lay);
            calendar_time = (TextView) itemView.findViewById(R.id.calendar_time);
        }
    }

    public interface OnItemClick {
        void setOnItemClick(View view, int pos);
    }
}
